package com.lanhai;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.log4j.Logger;

import com.alibaba.druid.pool.DruidDataSource;
import com.lanhai.javaBean.GuidepostBean;
import com.lanhai.model.Guidepost;
import com.lanhai.system.Accept4Agv;
import com.lanhai.system.MySystemTray;
import com.lanhai.utils.HexUtil;
import com.lanhai.utils.UDPUtil;


public class Main {
	
	private static final Logger logger = Logger.getLogger(Main.class);
	public static MySystemTray mst;
	public static Properties ppt;
	public static DruidDataSource dataSource;
	public static Accept4Agv ulis4Agv;
	
	public int agvSendPort = 9999;//向AGV发送数据时使用的本地端口号
	public static int port = 5000;//其它服务器的接收端口
	public String paramUrl;
	public String dataPath;//基础数据路径
	public String dbUrl;//数据库连接地址
	public String dbDriver;//数据库访问驱动
	
	public ExecutorService pool;
	
	/**
	 * GuidepostMap(路标)
	 */
	public Map<Integer, Guidepost> guidepostMap;	
	
	public static void main(String[] args) {
		mst = new MySystemTray();
		Main main = new Main();
		main.init();
		//new MySystemTray();
	}
	
	/**
	 * 初始化系统数据
	 */
	public void init(){
		ppt = new Properties();
		InputStream is = Main.class.getClass().getResourceAsStream("/jdbc.properties");
		try{
			ppt.load(is);	
			this.dbUrl = String.valueOf(ppt.getProperty("url"));
			this.dbDriver = String.valueOf(ppt.getProperty("driverClassName"));
			this.dataPath = String.valueOf(ppt.getProperty("dataPath"));
		}catch(Exception e){
			e.printStackTrace();
		}	
		logger.info("执行初始化方法！！！");
		//创建数据库连接池
		initDruid();
		loadBaseData();
		pool = Executors.newCachedThreadPool();
		initSocket(this);
		logger.info("初始化成功！！！");
	};
	
	/**
	 * 初始化数据库连接池
	 */
	public void initDruid(){
	    dataSource = new DruidDataSource(); // 创建Druid连接池
	    dataSource.setDriverClassName(dbDriver); // 设置连接池的数据库驱动
	    dataSource.setUrl(dbUrl); // 设置数据库的连接地址
	    //dataSource.setUsername(dbUserName); // 设置数据库的用户名
	    //dataSource.setPassword(dbPassword); // 设置数据库的密码
	    dataSource.setInitialSize(1); // 设置连接池的初始大小
	    dataSource.setMinIdle(1); // 设置连接池大小的下限
	    dataSource.setMaxActive(20); // 设置连接池大小的上限	
        //连接泄漏监测
	    //dataSource.setRemoveAbandoned(true);
	    //dataSource.setRemoveAbandonedTimeout(5);
		//配置获取连接等待超时的时间
	    dataSource.setMaxWait(20000);
		//配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
	    dataSource.setTimeBetweenEvictionRunsMillis(20000);
		//防止过期
	    dataSource.setValidationQuery("SELECT 'x'");
	    dataSource.setTestWhileIdle(true);
	    dataSource.setTestOnBorrow(true);  
	}
	
	/**
	 * 加载系统的基础数据
	 */
	public void loadBaseData(){
		//加载路标列表
		guidepostMap = new HashMap<Integer, Guidepost>();
		List<Guidepost> guidepost = GuidepostBean.loadData(dataSource);
		for(Guidepost gd:guidepost){
			guidepostMap.put(gd.getNum(), gd);
		}
	}
	
	/**
	 * 监听UDP端口
	 */
	public static void initSocket(Main main){
		int agvport = Integer.parseInt(ppt.getProperty("agvport"));
		ulis4Agv = new Accept4Agv(main, agvport);
		Thread ut = new Thread(ulis4Agv);
		ut.start();
	}
	
	public void handle(byte[] data, String ipAddress) throws Exception{
		int gpId = HexUtil.getGuidepost4Data(data);
		Guidepost gp = guidepostMap.get(gpId);
		logger.info(gp.getName() + ",ctrlIp:" + gp.getCtrlIp());
		String ctrlIp = gp.getCtrlIp();
		UDPUtil.sendData(data, ctrlIp, port, agvSendPort);
		logger.info(ipAddress + ":" + HexUtil.byte2HexStr(data));
	}
	
	public static void Exit(){
		logger.info("通过托盘菜单退出系统！");
		System.exit(0);
	}
	
}